Skip to content

Conversation

@wearzdk
Copy link

@wearzdk wearzdk commented Sep 2, 2025

The Problem

Currently, superjson's internal isURL check (payload instanceof URL) will throw a TypeError: Right-hand side of 'instanceof' is not an object in JavaScript environments where the global URL constructor is not available.

This issue occurs in environments like:

  • WeChat Mini Programs
  • Other non-standard JavaScript runtimes

This prevents users in these growing ecosystems from using superjson without applying workarounds like global polyfills or custom configuration overrides.

The Solution

This PR adds a guard clause (typeof URL !== 'undefined') before the instanceof check. By leveraging short-circuiting, this ensures that instanceof URL is only evaluated when the URL constructor actually exists.

  • In standard environments (Browsers, Node.js): The behavior is completely unchanged.
  • In environments without URL: The check now correctly and safely returns false instead of crashing.

This is a minimal, non-breaking change that significantly improves the library's robustness and compatibility.

@wearzdk wearzdk requested a review from Skn0tt as a code owner September 2, 2025 06:11
Copy link
Collaborator

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, I like it. Could you add a reproduction test? If you delete globalThis.URL at the beginning of the test, and reinstante at the end, you can simulate an environment that doesn't have URL.

@wearzdk
Copy link
Author

wearzdk commented Sep 2, 2025

Hi, I've just added a test following your suggestion to simulate an environment without URL. It no longer throws errors now. Looking forward to your review!

Skn0tt
Skn0tt previously approved these changes Sep 2, 2025
Copy link
Collaborator

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect, thanks!

@Skn0tt
Copy link
Collaborator

Skn0tt commented Sep 2, 2025

uh-oh, could you take a look at the linter?

@wearzdk
Copy link
Author

wearzdk commented Sep 3, 2025

Okay, I've fixed the lint issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants